<?php

namespace haibao\cms\business\emode;

use haibao\cms\data\emode\EmodeBrand as EmodeBrandData; 
use haibao\cms\data\emode\EmodeProduct as EmodeProductData;
use haibao\cms\data\emode\EmodeArticle as EmodeArticleData;
use haibao\cms\business\BusinessException;

class Emode extends \haibao\cms\business\Base{
   
    /**
     * 获取品牌列表
     * @param unknown $query
     */
   public static function getEmodeBrandList($query){
       $data = new EmodeBrandData();
       return $data->getEmodeBrandList($query);
   }
   
   /**
    * 保存单品
    * @param unknown $model
    */
   public static function saveEmodeProduct($model){
       if(!$model->ImageId){
           throw new BusinessException('单品图不能为空');
       }
       if(!$model->BrandId){
           throw new BusinessException('品牌名称不能为空');
       }
       if(strtotime($model->ShowTime) > strtotime("now")){
           throw new BusinessException('显示时间需小于等于当前时间');
       }
       self::checkOperation(self::FUNC_CODE_EMODE_ADD);
       $data = new EmodeProductData();
       $data->saveEmodeProduct($model);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_ADD, $model->toArray(true,true));
   }
   
   /**
    * 获取品牌对应单品数量
    * @return multitype:NULL
    */
   public static function getProductCount(){
       $data = new EmodeProductData();
       return $data->getProductCount();
   }
   
   /**
    * 获取品牌下单品列表
    * @param unknown $query
    */
   public static function getEmodeProductList($query){
       $data = new EmodeProductData();
       return $data->getEmodeProductList($query);
   }
   
   /**
    * 保存品牌
    */
   public static function saveEmodeBrand($model){
       if(!$model->Name){
           throw new BusinessException('品牌名称不能为空');
       }
       if(!$model->ImageId){
           throw new BusinessException('品牌LOGO不能为空');
       }
       if(strtotime($model->ShowTime) > strtotime("now")){
           throw new BusinessException('显示时间需小于等于当前时间');
       }
       self::checkOperation(self::FUNC_CODE_EMODE_ADD);
       $data = new EmodeBrandData();
       $data->saveEmodeBrand($model);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_ADD, $model->toArray(true,true));
   }
   
   /**
    * 通过单品id获取数据
    */
   public static function getEmodeProductById($productId){
       if(!$productId){
           throw new BusinessException('单品ID不能为空');
       }
       $data = new EmodeProductData();
       return $data->getEmodeProductById($productId);
   }
   
   /**
    * 通过品牌ID获取数据
    */
   public static function getEmodeBrandById($brandId){
       if(!$brandId){
           throw new BusinessException('品牌ID不能为空');
       }
       $data = new EmodeBrandData();
       return $data->getEmodeBrandById($brandId);
   }
   
   /**
    * 修改单品状态
    */
   public static function setProducStatus($id,$status){
       if(!$id){
           throw new BusinessException('单品ID不能为空');
       }
       if(!$status){
           $status = \haibao\cms\model\data\emode\EmodeProduct::STATUS_DELETE;
       }
       self::checkOperation(self::FUNC_CODE_EMODE_DELETE);
       $data = new EmodeProductData();
       $data->setProducStatus($id,$status);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_DELETE, $id);
   }
   
   /**
    * 修改品牌状态
    */
   public static function setBrandStatus($id,$status){
       if(!$id){
           throw new BusinessException('品牌ID不能为空');
       }
       if(!$status){
           $status = \haibao\cms\model\data\emode\EmodeBrand::STATUS_DELETE;
       }
       self::checkOperation(self::FUNC_CODE_EMODE_DELETE);
       $data = new EmodeBrandData();
       $data->setBrandStatus($id,$status);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_DELETE, $id);
   }
   
   /**
    * 获取emode文章列表
    */
   public static function getEmodeArticleList($query){
       $data = new EmodeArticleData();
       return $data->getEmodeArticleList($query);
   }
   
   public static function getEmodeArticle($page,$lastArticleId){
       $data = new EmodeArticleData();
       return $data->getEmodeArticle($page,$lastArticleId);
   }
   
   /**
    * 保存文章
    */
   public static function saveEmodeArticle($model){
       if(!$model->Title){
           throw new BusinessException('文章标题不能为空');
       }
       if(!$model->ImageId){
           throw new BusinessException('文章题图不能为空');
       }
       if(!$model->PublishTime){
           throw new BusinessException('发布时间不能为空');
       }
       if(!$model->Id){
           $model->CreateBy = self::getCurrentUser()->Id;
           $model->CreateTime  = new \DateTime();
       }else {
           $article = self::getOneArticleById($model->Id);
           $model->CreateBy = $article['CreateBy'];
           $model->CreateTime = $article['CreateTime'];
       }
       if($model->Id && $model->IsPublish == \haibao\cms\model\data\emode\EmodeArticle::PUBLISHED_YES){
           if(strtotime($model->PublishTime) > strtotime("now")){
               throw new BusinessException('发布时间需小于等于当前时间');
           }
       }
       self::checkOperation(self::FUNC_CODE_EMODE_ARTICLE_ADD);
       $data = new EmodeArticleData();
       $data->saveEmodeArticle($model);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_ARTICLE_ADD, $model->toArray(true,true));
   }
   
   /**
    * 通过文章id获取数据
    */
   public static function getOneArticleById($id){
       if(!$id){
           throw new BusinessException('文章ID不能为空');
       }
       $data = new EmodeArticleData();
       return $data->getOneArticleById($id);
   }
   
   /**
    * 删除文章
    */
   public static function delArticle($articleId,$status){
       if(!$articleId){
           throw new BusinessException('文章ID不能为空');
       }
       if(!$status){
           $status = \haibao\cms\model\data\emode\EmodeArticle::STATUS_DELETE;
       }
       self::checkOperation(self::FUNC_CODE_EMODE_ARTICLE_DELETE);
       $data = new EmodeArticleData();
       $data->delArticle($articleId,$status);
       self::saveOperateHistory(self::FUNC_CODE_EMODE_ARTICLE_DELETE, $articleId);
   }
   
   
}